package com.example.alex_zhang.addsetting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.utils.PermissionsUtils;

class AlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater inflater;
    private final Context mContext;
    private MyAdapter.ContentViewHolder contentViewHolder;
    private ArrayList<String> data;
    public static final int CONTENT_FLAG = 0;
    public static final int FOOTER_FLAG = 1;
    private int currentIndex = 0;
    private int max = 5;

    public AlbumAdapter(Context context, MyAdapter.ContentViewHolder contentViewHolder) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.contentViewHolder = contentViewHolder;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == CONTENT_FLAG) {
            return new AlbumAdapter.AlbumViewHolder(inflater.inflate(R.layout.item_album, viewGroup, false));
        }
        return new FooterViewHolder(inflater.inflate(R.layout.item_album, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == FOOTER_FLAG) {
            if (data != null && data.size() == max) {
                if (holder instanceof FooterViewHolder) {
                    ((FooterViewHolder) holder).ivAlbum.setVisibility(View.GONE);
                }
            } else {
                if (holder instanceof FooterViewHolder) {
                    ((FooterViewHolder) holder).ivAlbum.setOnClickListener(((FooterViewHolder) holder));
                    ((FooterViewHolder) holder).ivAlbum.setVisibility(View.VISIBLE);
                }
            }
        } else {
            if (data == null) {
                if (holder instanceof FooterViewHolder) {
                    ((FooterViewHolder) holder).ivAlbum.setOnClickListener(((FooterViewHolder) holder));
                    ((FooterViewHolder) holder).ivAlbum.setVisibility(View.VISIBLE);
                }
            } else {
                String url = data.get(position);
                if (holder instanceof AlbumViewHolder) {
                    Glide.with(mContext)
                            .load(url)
                            .into(((AlbumViewHolder) holder).ivAlbum);
                }
            }

        }

    }

    @Override
    public int getItemCount() {
        return data == null ? 1 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return FOOTER_FLAG;
        }
        return CONTENT_FLAG;
    }

    public void setData(ArrayList<String> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void openAlbum(ArrayList<String> data) {
        PhotoPicker.builder()
                .setPhotoCount(max)
                .setShowCamera(true)
                .setShowGif(true)
                .setPreviewEnabled(false)
                .setSelected(data)
                .start((MainActivity) mContext, PhotoPicker.REQUEST_CODE);
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final ImageView ivAlbum;

        FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAlbum = itemView.findViewById(R.id.iv_album);
            ivAlbum.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.ic_launcher));
        }

        @Override
        public void onClick(View v) {
            currentIndex = contentViewHolder.getAdapterPosition();
            Log.d("onClick", "onClick: " + currentIndex);
            if (listener != null) {
                listener.onSelectItemClickListener(currentIndex);
            }
            openAlbum(data);
        }
    }

    OnSelectItemClickListener listener;

    public void setOnSelectItemClickListener(OnSelectItemClickListener listener) {
        this.listener = listener;
    }

    public List<String> getData() {
        return data;
    }

    public int getPosition() {
        return currentIndex;
    }

    public class AlbumViewHolder extends RecyclerView.ViewHolder {

        private final ImageView ivAlbum;

        public AlbumViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAlbum = itemView.findViewById(R.id.iv_album);
        }
    }
}
